package com.manda.smart.hub.common.comparator;

import cn.hutool.core.annotation.AnnotationUtil;
import com.manda.smart.hub.common.annotation.Priority;

import java.util.Comparator;
import java.util.Objects;

/**
 * @author hongda.li
 */
public interface Sortable<T> {

    void sort(Comparator<T> comparator);

    default void sort() {
        this.sort(Comparator.comparingInt(strategy -> this.getPriority(strategy.getClass())));
    }

    default int getPriority(Class<?> clazz) {
        Objects.requireNonNull(clazz);
        Priority priority = AnnotationUtil.getAnnotation(clazz, Priority.class);
        if (priority == null) {
            return Priority.NORMAL_PRIORITY;
        } else {
            return priority.value();
        }
    }
}
